#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <iostream>
using namespace std;

class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        if (arr.size() == 0) return;
        int cur = 0, dest = -1;
        while (dest < (int)(arr.size() - 1)) {
            if (arr[cur] == 0)
                dest += 2;
            else
                dest += 1;
            cur++;
        }
        cur--;
        if (dest == arr.size()) {
            cur--;
            arr[arr.size() - 1] = 0;
            dest -= 2;
        }
        while (cur >= 0) {
            if (arr[cur] != 0) {
                arr[dest] = arr[cur];
                dest--;
            }
            else {
                arr[dest] = 0;
                arr[dest - 1] = 0;
                dest -= 2;
            }
            cur--;
        }
    }
};